package org.blogwu.learn.multithread.chapter2.demo9;

/**
 * 使用Integer导致线程安全问题
 *
 * @author hyf
 * @date 2018-09-16
 **/
public class Main1 {
    static Integer i = new Integer(0);

    static class AddThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                synchronized (Main1.i) {
                    Main1.i++;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        AddThread addThread = new AddThread();
        AddThread addThread1 = new AddThread();
        addThread.start();
        addThread1.start();
        addThread.join();
        addThread1.join();
        System.out.println(i);
    }
}